<?php

namespace app\api\controller;

use think\Env;
use app\common\controller\Api;
use app\admin\model\Banner as BannerModel;
use app\admin\model\Module as ModuleModel;
use app\admin\model\Notice as NoticeModel;
use app\admin\model\AboutUs as AboutUsModel;
use app\admin\model\Introduction as IntroductionModel;
use app\admin\model\News as NewsModel;
use app\admin\model\Commodity as CommodityModel;
use app\admin\model\CommoditySkuValue as CommoditySkuValueModel;

class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    //首页
    public function Index()
    {
        $resultArr = [
            'banner' => $this->banner(), //首页轮播
            'modele' => $this->modele(),     //金刚区模块名
            'notice' => $this->notice(), //公告
            'aboutUs' => $this->aboutUs(), //关于我们、在线客服、玩法介绍、在线客服
            'news' => $this->news(),//行业资讯、活动专区
            'exchange' => $this->exchange(),//首页积分兑换
        ];
        return sucessMsg(200, '请求成功', $resultArr);
    }

    //轮播
    public function banner()
    {
        $model = new BannerModel();
        $list = $model
            ->field('id,image')
            ->select();
        foreach ($list as $v){
            $v['image'] = Env::get('database.domin') . $v['image'];
        }
        return $list;
    }

    //金刚区模块名
    public function modele()
    {
        $model = new ModuleModel();
        $list = $model
            ->where('type', 1)
            ->field('id,name,image')
            ->select();
        $list = addWebSiteUrl($list, ['image']);
        return $list;
    }

    //金刚区
    public function aboutUs()
    {
        $model = new AboutUsModel();
        //关于我们
        $list = $model
            ->where('type', 1)
            ->field('id,content')
            ->select();
        //在线客服
        $list1 = $model
            ->where('type', 2)
            ->field('id,content')
            ->select();
        //玩法介绍
        $model = new IntroductionModel();
        $list2 = $model
            ->where('type', 1)
            ->field('id,title,createtime,image')
            ->select();
        $list2 = addWebSiteUrl($list2, ['image']);
        //新人帮助
        $list3 = $model
            ->where('type', 2)
            ->field('id,title,createtime,image')
            ->select();
        $list3 = addWebSiteUrl($list3, ['image']);
        return [
            'list' => $list,
            'list1' => $list1,
            'list2' => $list2,
            'list3' => $list3,
        ];
    }

    //公告
    public function notice()
    {
        $model = new NoticeModel();
        //首页标题
        $list = $model
            ->field('id,title')
            ->select();
        //公告列表
        $list1 = $model
            ->field('id,title,createtime,image')
            ->select();
        $list1 = addWebSiteUrl($list1, ['image']);
        return [
            'list' => $list,
            'list1' => $list1,
        ];
    }

    //首页积分兑换
    public function exchange()
    {
        $model = new CommodityModel();
        $list = $model
            ->field('id,title,image,integral')
            ->select();
        $list = addWebSiteUrl($list, ['image']);
        return $list;
    }

    //行业资讯、活动专区
    public function news()
    {
        $model = new NewsModel();
        //行业资讯
        $list = $model
            ->where('type', 1)
            ->field('id,title,createtime,image')
            ->select();
        $list = addWebSiteUrl($list, ['image']);
        //活动专区
        $list1 = $model
            ->where('type', 1)
            ->field('id,title,createtime,image')
            ->select();
        $list1 = addWebSiteUrl($list1, ['image']);
        return [
            'list' => $list,
            'list1' => $list1
        ];
    }

    //首页公告列表详情页
    public function noticeDetails()
    {
        $id = input('id');
        if (empty($id)) {
            return errMsg(422, '请输入公告列表id!');
        }
        $model = new NoticeModel();
        $list = $model
            ->where('id', $id)
            ->field('id, title, content, image, FROM_UNIXTIME(createtime,"%Y-%m-%d %h:%i") as createtime')
            ->find();
        $list = addWebSiteUrl($list, ['image']);
        return sucessMsg(200, '请求成功', $list);
    }

    //首页玩法介绍详情页
    public function playDetails()
    {
        $id = input('id');
        if (empty($id)) {
            return errMsg(422, '请输入玩法介绍id!');
        }
        $model = new IntroductionModel();
        $list = $model
            ->where('id', $id)
            ->field('id, title, content, image, FROM_UNIXTIME(createtime,"%Y-%m-%d %h:%i") as createtime, type')
            ->find();
        $list = addWebSiteUrl($list, ['image']);
        return sucessMsg(200, '请求成功', $list);
    }

    //首页新人帮助详情页
    public function helpDetails()
    {
        $id = input('id');
        if (empty($id)) {
            return errMsg(422, '请输入新人帮助id!');
        }
        $model = new IntroductionModel();
        $list = $model
            ->where('id', $id)
            ->field('id, title, content, image, FROM_UNIXTIME(createtime,"%Y-%m-%d %h:%i") as createtime, type')
            ->find();
        $list = addWebSiteUrl($list, ['image']);
        return sucessMsg(200, '请求成功', $list);
    }

    //首页行业资讯详情页
    public function informationDetails()
    {
        $id = input('id');
        if (empty($id)) {
            return errMsg(422, '请输入行业资讯id!');
        }
        $model = new NewsModel();
        $list = $model
            ->where('type', 1)
            ->where('id', $id)
            ->field('id,title,content,image')
            ->find();
        if (empty($list)) {
            return errMsg(422, '找不到数据');
        }
        $list->image = Env::get('database.domin') . $list->image;
        return sucessMsg(200, '详情获取成功！', ['list' => $list]);
    }

    //首页活动专区详情页
    public function activityDetails()
    {
        $id = input('id');
        if (empty($id)) {
            return errMsg(422, '请输入活动专区id!');
        }
        $model = new NewsModel();
        $list = $model
            ->where('type', 2)
            ->where('id', $id)
            ->field('id,title,content,image')
            ->find();
        if (empty($list)) {
            return errMsg(422, '找不到数据');
        }
        $list->image = Env::get('database.domin') . $list->image;
        return sucessMsg(200, '详情获取成功！', ['list' => $list]);
    }

    //搜索
    public function search()
    {
        $where = input('title');
        if (empty($where)) {
            $this->error('请输入商品名称！', [], 0);
        }
        $model = new CommodityModel();
        $data = $model->where(['status_switch' => 1])
            ->where(function ($query) use ($where) {
                if ($where) {
                    $query->where('title', 'like', '%' . $where . '%');
                }
            })
            ->select();
        foreach ($data as &$v) {
            $v['image'] = Env::get('database.domin') . $v['image'];
        }
        $this->success('获取成功', $data, 1);
    }

    //首页积分兑换商品详情页
    public function exchangeDetails()
    {
        $id = input('id');
        if (empty($id)) {
            return errMsg(422, '请输入商品id!');
        }
        $model = new CommodityModel();
        $list = $model
            ->where('id', $id)
            ->field('id,title,content,image,brief,integral,original,num,ensure,place')
            ->find();
        if (empty($list)) {
            return errMsg(422, '找不到数据');
        }
        $list->image = Env::get('database.domin') . $list->image;
        return sucessMsg(200, '详情获取成功！', ['list' => $list]);
    }
}
